import time
import pyautogui

from my_package.QQMailSender import QQMailSender
from my_package.file_utils import find_image_files
from my_package.ring_pk import pk, sign_up, sign_up_fast
from my_package.buy_ticket import do_buy_ticket
from my_package.xqol_utils import get_game_windows
from my_package.constants import email_map

def main_pk_vip(main_account_list=None,
                sub_account_list=None,
                num=40,
                append_count=0,
                send_email=True,
                save_pk_end_img=True,
                save_sign_up_img=True,
                buy_ticket=True):
    """
    【请优先使用此方法，后续经常维护优化该方法，等待完全稳定！！！！】
    TODO: 必传经常存在小号入场券被用完了，(可能有其它用户进入了擂台，参与匹配，导致小号与其匹配上了)
    导致主号无法正常完成40次pk。
    :param send_email:
    :param main_account_list: 主号账号
    :param sub_account_list: 副号账号
    :param num: 主号购买的券以及需要完成的PK次数，副号购买券的数量
    :param append_count: 副号购买入场券时，额外累加的数值，默认0，一般推荐追加15个
    :param save_pk_end_img: 是否保存比武结束时的图片
    :param save_sign_up_img
    :param buy_ticket: 是否购买入场券，默认购买
    :return:
    """

    if main_account_list is None or sub_account_list is None:
        print("请配置需要进行队列PK的主号，以及相关副号")
        return
    if main_account_list is None:
        main_account_list = []
    if sub_account_list is None:
        sub_account_list = []
    # 主窗口
    windows = get_game_windows(main_account_list)
    if len(windows) == 0:
        print("未找到主号窗口，请先启动")
        exit()
    # 副窗口
    sub_windows = get_game_windows(sub_account_list)
    if len(sub_windows) == 0:
        print("未找到副号窗口，请先启动")
        exit()
    # 初始化发送器 (需替换为实际QQ邮箱和授权码)
    mailer = QQMailSender(
        # 发件人
        sender_name="艾克",
        sender_email="2287474280@qq.com",
        auth_code="vkdrkvkeudmcecdb"  # 在QQ邮箱设置中获取的SMTP授权码
    )
    # 循环以上窗口
    for window in windows:
        qq_email = email_map[window.title]
        # 激活窗口
        try:
            # 将窗口激活并置顶
            window.activate()
        except Exception as e:
            print("窗口操作失败：", e)
        print("-------------------- 主号", window.title, "开始擂台 --------------------")

        # 定义主号PK的场次
        count = 0
        # 定义主号处于比武结束场景时，是否点击取消按钮，而不是确认按钮
        click_cancel_btn = False

        if buy_ticket:
            print("-------------------- 主号", window.title, "准备购买 入场券 --------------------")
            # 主号购买入场券
            do_buy_ticket(window, num)
            print("-------------------- 主号", window.title, "完成购买 入场券 --------------------")

            sub_titles = [window.title for window in sub_windows]
            print("-------------------- 副号", sub_titles, "准备购买 入场券 --------------------")
            # 副号全部购买入场券
            for sub_window in sub_windows:
                print("-------------------- 小号", sub_window.title, "准备购买 入场券 --------------------")
                do_buy_ticket(sub_window, num + append_count)
                print("-------------------- 小号", sub_window.title, "完成购买 入场券 --------------------")

        # TODO 等待调试测试！！！
        print("-------------------- 主号和副号 准备报名 --------------------")
        sign_up_windows = [window for window in sub_windows]
        sign_up_windows.append(window)
        successful_windows = sign_up_fast(sign_up_windows)
        print("报名成功窗口：", successful_windows)
        success_titles = [window.title for window in successful_windows]
        if len(success_titles) > 0:
            if send_email:
                if save_sign_up_img:
                    mailer.send_images_via_email(
                        to_email=email_map[window.title],
                        subject=window.title + "报名成功",
                        folder_name=window.title + "/sign_up"
                    )
                else:
                    mailer.send_email_text(
                        # 收件人
                        to_email=qq_email,  # 可以多个收件人
                        subject=window.title + "报名成功",
                        content=",".join(success_titles)
        )
        print("-------------------- 主号和副号 完成报名 --------------------")

        print("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 主号", window.title, "开始PK逻辑 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓")
        while True:
            if count == num - 1:
                click_cancel_btn = True
                print("主号", window.title, "已进入", count, "次，即将点击取消按钮")
            flag1 = pk(window=window, msg=False, is_click_cancel_btn=click_cancel_btn, save_pk_end_img=save_pk_end_img, count=count)
            for sub_window in sub_windows:
                pk(sub_window, False, click_cancel_btn)
            if flag1:
                count += 1
                print("=====================", window.title, "主号战斗结束了。完成", count, "次匹配 =====================")
                if count >= num:
                    print("=====================", window.title, "主号已达到", num, "次匹配，跳出循环，进入下一个账号 =====================")
                    break
        tips = "↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ 主号", window.title, "已结束匹配。匹配次数", count, "次 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑"
        if send_email:
            if save_pk_end_img:
                mailer.send_images_via_email(
                    to_email=email_map[window.title],
                    subject=window.title + "擂台匹配完成",
                    folder_name=window.title
                )
            else:
                # 发送文本邮件
                mailer.send_email_text(
                    # 收件人
                    to_email=email_map[window.title],  # 可以多个收件人5
                    subject=window.title + "匹配完成",
                    content=str(tips)
                )
        print(tips)

if __name__ == '__main__':
    main_pk_vip()